#include "mainwindow.h"

#include <QDebug>
#include <QApplication>
#include <spdlog/spdlog.h>
#include <cstdio>

void customMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
    QByteArray localMsg = msg.toLocal8Bit();
    const char *file = context.file ? context.file : "";
    const char *function = context.function ? context.function : "";
    QString message{QString().sprintf("message %s (file %s: %u line, in function: %s)", localMsg.constData(), file, context.line, function)};

    auto print_message = [&](QString info_level){
        fprintf(stderr, "%s", (info_level + message).toStdString().c_str());
    };

    switch (type)
    {
    case QtDebugMsg:    //调试信息提示
        print_message("Debug: ");
        spdlog::debug(message.toStdString());
        break;
    case QtInfoMsg:
        print_message("Info: ");
        spdlog::info(message.toStdString());
        break;
    case QtWarningMsg:
        print_message("Warning: ");
        spdlog::warn(message.toStdString());
        break;
    case QtCriticalMsg:
        print_message("Critical: ");
        spdlog::critical(message.toStdString());
        break;
    case QtFatalMsg:
        print_message("Fatal: ");
        spdlog::error(message.toStdString());
        break;
    }
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    qInstallMessageHandler(customMessageHandler); //注册MsgHandler回调函数
    MainWindow w;
    w.show();
    return a.exec();
}

